/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ELE.EnterpriseLibrary.Common.Configuration.Design
{
  /// <summary>
  /// A windows form for selecting a type from a loaded assembly.
  /// Works in conjuction with <see cref="AssemblyTypeSelectorEditor"/>.
  /// </summary>
  public partial class AssemblyTypeSelectorUI : Form
  {
    private Assembly assembly;
    /// <summary>
    /// Default constructor
    /// </summary>
    public AssemblyTypeSelectorUI()
    {
      InitializeComponent();
    }
    /// <summary>
    /// This constructor is used to initialize the window with the right list of types from the given assembly.
    /// </summary>
    /// <param name="assembly"></param>
    public AssemblyTypeSelectorUI(Assembly assembly ) : this()
    {
      this.assembly = assembly;
      this.listBoxTypes.BeginUpdate();
      if (assembly != null)
      {
        Type[] types = assembly.GetTypes();
        foreach(Type type in types)
        {
          this.listBoxTypes.Items.Add(type.FullName);
        }
      }
      this.listBoxTypes.EndUpdate();
    }

    private void buttonSelect_Click(object sender, EventArgs e)
    {
      DialogResult = DialogResult.OK;
      Close();
    }
    /// <summary>
    /// The selected type
    /// </summary>
    public Type SelectedType
    {
      get
      {
        return assembly.GetType(this.listBoxTypes.SelectedItem.ToString());
      }
    }
  }
}